import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  {
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    name: 'System',
    meta: { title: '系统管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/system/user'),
        meta: { title: '用户管理', icon: 'table' }
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/system/role'),
        meta: { title: '角色管理', icon: 'table' }
      },
      {
        path: 'permission',
        name: 'Permission',
        component: () => import('@/views/system/permission'),
        meta: { title: '路由管理', icon: 'table' }
      }
    ]
  },
  {
    path: '/elastic',
    component: Layout,
    redirect: '/elastic',
    children: [{
      path: 'list',
      name: 'Elastic',
      component: () => import('@/views/elastic'),
      meta: { title: '全局数据管理', icon: 'tree' }
    }]
  },
  {
    path: '/category',
    component: Layout,
    redirect: '/category',
    children: [{
      path: 'list',
      name: 'Category',
      component: () => import('@/views/category'),
      meta: { title: '分类管理', icon: 'tree' }
    }]
  },
  {
    path: '/banner',
    component: Layout,
    redirect: '/banner',
    children: [{
      path: 'list',
      name: 'Banner',
      component: () => import('@/views/banner'),
      meta: { title: '轮播图管理', icon: 'nested' }
    }]
  },
  {
    path: '/member',
    component: Layout,
    redirect: '/member/memberInfo',
    name: 'Member',
    meta: { title: '会员管理', icon: 'tree' },
    children: [
      {
        path: 'memberInfo',
        name: 'MemberInfo',
        component: () => import('@/views/member/info'),
        meta: { title: '会员信息管理', icon: 'form' }
      },
      {
        path: 'memberCourse',
        name: 'MemberCourse',
        component: () => import('@/views/member/course'),
        meta: { title: '会员课程管理', icon: 'form' }
      },
      {
        path: 'memberBlog',
        name: 'MemberBlog',
        component: () => import('@/views/member/blog'),
        meta: { title: '会员文章管理', icon: 'form' }
      },
      {
        path: 'memberComment',
        name: 'MemberComment',
        component: () => import('@/views/member/comment'),
        meta: { title: '会员评论管理', icon: 'form' }
      },
      {
        path: 'memberFans',
        name: 'MemberFans',
        component: () => import('@/views/member/fans'),
        meta: { title: '会员关注管理', icon: 'form' }
      },
    ]
  },
  {
    path: '/course',
    component: Layout,
    redirect: '/course/courseType',
    name: 'Course',
    meta: { title: '课程管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'courseType',
        name: 'CourseType',
        component: () => import('@/views/course/courseType'),
        meta: { title: '课程分类管理', icon: 'link' }
      },
      {
        path: 'courseDetail',
        name: 'CourseDetail',
        component: () => import('@/views/course/courseDetail'),
        meta: { title: '课程详情管理', icon: 'link' }
      },
      {
        path: 'courseKnowledge',
        name: 'CourseKnowledge',
        component: () => import('@/views/course/courseKnowledge'),
        meta: { title: '课程知识管理', icon: 'link' }
      }
      ,
      {
        path: 'courseChapter',
        name: 'CourseChapter',
        component: () => import('@/views/course/courseChapter'),
        meta: { title: '课程章节管理', icon: 'link' }
      }
    ]
  },
  {
    path: '/teacher',
    component: Layout,
    redirect: '/teacher',
    children: [{
      path: 'list',
      name: 'Teacher',
      component: () => import('@/views/teacher'),
      meta: { title: '讲师管理', icon: 'nested' }
    }]
  },
  {
    path: '/blog',
    component: Layout,
    redirect: '/blog',
    children: [{
      path: 'list',
      name: 'Blog',
      component: () => import('@/views/blog'),
      meta: { title: '文章管理', icon: 'nested' }
    }]
  },
  {
    path: '/guide',
    component: Layout,
    redirect: '/guide',
    children: [{
      path: 'list',
      name: 'Guide',
      component: () => import('@/views/guide'),
      meta: { title: '导航管理', icon: 'link' }
    }]
  },
  {
    path: '/resource',
    component: Layout,
    redirect: '/resource',
    children: [{
      path: 'list',
      name: 'Resource',
      component: () => import('@/views/resource'),
      meta: { title: '资源管理', icon: 'tree' }
    }]
  },

  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', 
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
